const fs = require('fs');

// 从文档中提取的城市数据
const cityData = [
    // 许昌区域 (areaId: 100)
    { id: 289, name: "许昌" },
    { id: 302, name: "洛阳" },
    { id: 304, name: "长安" },

    // 许昌子区域
    { id: 2, name: "新手村(1 级)" },
    { id: 301, name: "陈留(5 级)" },
    { id: 487, name: "九里山(20 级)" },
    { id: 533, name: "芒砀山(30 级)" },

    // 洛阳子区域
    { id: 285, name: "官渡(40 级)" },
    { id: 286, name: "虎牢关(50 级)" },
    { id: 333, name: "乌巢(80 级)" },
    { id: 337, name: "函谷关(55 级)" },

    // 长安子区域
    { id: 303, name: "华山(90 级)" },
    { id: 311, name: "潼关(75 级)" },
    { id: 414, name: "五丈原(65 级)" },

    // 成都区域 (areaId: 19)
    { id: 70, name: "成都" },
    { id: 111, name: "南中" },
    { id: 117, name: "汉中" },

    // 成都子区域
    { id: 93, name: "落凤坡(30 级)" },
    { id: 112, name: "广汉(40 级)" },
    { id: 113, name: "峨眉山(50 级)" },

    // 南中子区域
    { id: 52, name: "云南(60 级)" },
    { id: 143, name: "泸水(70 级)" },
    { id: 351, name: "西洱湖(80 级)" },

    // 汉中子区域
    { id: 306, name: "子午谷(90 级)" },
    { id: 307, name: "街亭(100 级)" },
    { id: 308, name: "上方谷(120 级)" },

    // 建业区域 (areaId: 43)
    { id: 154, name: "建业" },
    { id: 212, name: "长沙" },
    { id: 264, name: "江陵" },

    // 建业子区域
    { id: 267, name: "广陵" },

    // 长沙子区域
    { id: 220, name: "巴陵" },
    { id: 221, name: "洞庭湖" },
    { id: 222, name: "湘水(130 级)" },

    // 江陵子区域
    { id: 137, name: "麦城(140 级)" },
    { id: 161, name: "长坂坡(140 级)" },
    { id: 163, name: "夷陵" }
];

// 去重函数 - 根据ID去重
function removeDuplicates(data) {
    const seen = new Set();
    return data.filter(item => {
        if (seen.has(item.id)) {
            return false;
        }
        seen.add(item.id);
        return true;
    });
}

// 去重
const uniqueCities = removeDuplicates(cityData);

// 按ID排序
uniqueCities.sort((a, b) => a.id - b.id);

// 创建输出对象
const output = {
    total_count: uniqueCities.length,
    cities: uniqueCities,
    extracted_at: new Date().toISOString(),
    source: "地图表格查询.md"
};

// 保存为JSON文件
const outputPath = 'output/cities_extracted.json';
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2), 'utf8');

console.log(`成功提取 ${uniqueCities.length} 个城市信息`);
console.log(`数据已保存到: ${outputPath}`);

// 显示前几个城市作为示例
console.log('\n前10个城市示例:');
uniqueCities.slice(0, 10).forEach(city => {
    console.log(`ID: ${city.id}, 名称: ${city.name}`);
});
